package com.lmm.lcuser.lc_user.plugins;


import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.lmm.dresswisdom.lmmframe.reactnative.BasePlugin;
import com.lmm.dresswisdom.lmmframe.reactnative.annotation.RNPlugin;
import com.lmm.lcuser.lc_user.MainActivity;
import com.lmm.lcuser.lc_user.dbmodel.LogCategoryRealm;
import com.lmm.lcuser.lc_user.utils.RealmUtils;

import java.util.Map;

import io.realm.RealmResults;

@RNPlugin(method = "getLogCategory")
public class GetLogCategoryPlugin extends BasePlugin<MainActivity> {
    public GetLogCategoryPlugin(MainActivity act) {
        super(act);
    }

    @Override
    protected void doAction(Map<String, Object> map, Callback callback) {

        RealmResults<LogCategoryRealm> results = RealmUtils.getRealm().where(LogCategoryRealm.class).findAll();
        WritableArray list = Arguments.createArray();
        for (LogCategoryRealm item : results) {
            list.pushMap(getMap(item));
        }
        WritableMap resmap = Arguments.createMap();
        resmap.putArray("list", list);
        callback.invoke(resmap);
    }


    private WritableMap getMap(LogCategoryRealm logCategoryRealm) {
        WritableMap map = Arguments.createMap();
        map.putString("catCode", logCategoryRealm.getCatCode());
        map.putString("catName", logCategoryRealm.getCatName());
        map.putString("defaultContent", logCategoryRealm.getDefaultContent());
        map.putString("icon", logCategoryRealm.getIcon());
        map.putInt("id", logCategoryRealm.getId());
        map.putInt("parentId", logCategoryRealm.getParentId());
        return map;
    }

}
